using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using Unity.VisualScripting;
using UnityEngine;

public class TriggerSkillCollider : FreeListObj
{
    public HashSet<GameObject> targetsIn = new();
    public HashSet<GameObject> targets = new();
    public HashSet<GameObject> targetsOut = new();

    protected bool canFeedback;
    protected Character owner;
    protected TeamType thisType;
    protected float radius;

    public virtual void Initialize(SkillData data, float radius = -1, bool isShow = true)
    {
        owner = data.Source;
        thisType = owner.teamType;
        this.radius = radius;
        GetComponent<SpriteRenderer>().enabled = isShow;

        canFeedback = true;
        owner.OnThisDeath += () => { canFeedback = false; };
    }

    public override void Destroy()
    {
        owner.OnThisDeath -= () => { canFeedback = false; };
        targetsIn.Clear();
        targets.Clear();
        targetsOut.Clear();
        base.Destroy();
    }

    protected virtual void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.GetComponent<Damageable>())
            if (collision.GetComponent<Damageable>().teamType != thisType)
            {
                targetsIn.Add(collision.gameObject);
                //Debug.Log(targetsIn + " len = " + targetsIn.Count);
            }
    }

    protected virtual void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.GetComponent<Damageable>())
            if (collision.GetComponent<Damageable>())
                if (collision.GetComponent<Damageable>().teamType != thisType)
                {
                    targets.Add(collision.gameObject);
                    //Debug.Log(enemies + " len = " + enemies.Count);
                }
    }

    protected virtual void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.GetComponent<Damageable>())
            if (collision.GetComponent<Damageable>().teamType != thisType)
            {
                targetsOut.Add(collision.gameObject);
                if (targets.Contains(collision.gameObject))
                    targets.Remove(collision.gameObject);
                //Debug.Log(enemiesEnd + " len = " + enemiesEnd.Count);
            }
    }
}
